//
//  CZStatusRetweetView.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit

class CZStatusRetweetView: UIView {
    
    //记录约束
    var retweetViewCons: Constraint?
    
    //定义属性 供外界赋值
    var statusViewModel: CZStatusViewModel?{
        didSet{
            
            //转发正文
            contentLabel.text = statusViewModel?.retweetContentText
            
            // 卸载约束
            /*
             - 如果配图数组. count > 0 就代表有配图
             - 赋值
             - 显示配图
             - 转发微博的 bottom == 配图的 bottom
             0 如果没有配图
             - 隐藏配图
             - 转发微博的 bottom == 文字的bottom
             */
            // 卸载约束
            retweetViewCons?.uninstall()
            // 有配图
            if (statusViewModel?.statusModel?.retweeted_status?.pic_urls?.count)! > 0 {
                pictureView.picUrls = statusViewModel?.statusModel?.retweeted_status?.pic_urls
                pictureView.isHidden = false
                
                self.snp_makeConstraints { (make) in
                    self.retweetViewCons = make.bottom.equalTo(pictureView).offset(10).constraint
                }
            }else {
                // 没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints { (make) in
                    self.retweetViewCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    //搭建界面
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.top.left.equalTo(10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }

        self.snp_makeConstraints { (make) in
            self.retweetViewCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //懒加载控件
    //转发正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGray, fontSize: CZNormalFontSize, text: "dahskdhakshdkashdjkashdjasjkd", maxWidth: CZSCREEN_W - 20)
    
    // 配图
    private lazy var pictureView: CZStatusPictrueView = {
        let view = CZStatusPictrueView()
        view.backgroundColor = CZRandomColor()
        return view
    }()
}
